home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Split Filename
- Date: 25 Jan 1996 19:54:12 GMT
- Organization: Internet Access Group, Orlando, Florida
- Distribution: world
- Message-ID: <4e8n54$h2s@news.iag.net>
- References: <4e6lsr$8ar@news1.radix.net>
- NNTP-Posting-Host: pm3-orl17.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4e6lsr$8ar@news1.radix.net>, jfw@radix.net says...
- >
- >Is there a C function that will split
- >/home/file.c into /home and file.c ?
- >
- >Thanks,
- >
- >Jim Ward
-
- No ansi c function, but you may find strrchr useful for this. The
- following code makes a lot of assumptions (ie, it only works safely, because
- I know the contents of buf ahead of time and they can't change). To be
- useful you will need to incorporate a number of safety checks in your code
- to protect your array bounds, check for invalid entries, etc.
-
- #include <stdio.h>
- #include <string.h>
-
- int main()
- {
- char *ptr, file[13], path[50], buf[]= "/root/sub/file.nam";
-
- ptr= strrchr( buf, '/');
- *ptr = '\0';
- ptr++;
-
- strcpy( path, buf);
- strcpy( file, ptr);
-
- printf( "The path is: %s\n", path);
- printf( "The file name is: %s\n", file);
-
- return 0;
- }
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-